﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Custom_Default : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer }; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!IsPostBack)
        {
            if (Request.QueryString["Name"] != null)
            {
                CustomPage = CustomAdminPage.GetByNameAndSiteId(Request.QueryString["Name"], AcmsContext.SiteId);
                if (CustomPage != null)
                {
                    listCustom.Visible = false;

                    string controlPath = CustomPage.ControlPath;

                    if (!string.IsNullOrEmpty(Request.QueryString["Path"]))
                    {
                        controlPath = Request.QueryString["Path"];
                    }

                    VirtualControlPath = controlPath;
                }
            }
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        breadcrumbs.Add("Data", null);
        if (CustomPage != null)
        {
            breadcrumbs.Add(CustomPage.Name, null);
        }
    }

    protected override void CreateChildControls()
    {
        //if loading control load it
        if (!string.IsNullOrEmpty(VirtualControlPath))
        {
            Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl tmp = Page.LoadControl(VirtualControlPath) as Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl;
            if (tmp != null)
            {
                tmp.AcmsContext = AcmsContext;
                tmp.User = this.User;
                phCustomControl.Controls.Add(tmp);
            }
            else
            {
                throw new Exception("BaseCustomAdminControl");
            }
        }
        base.CreateChildControls();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCustomPages.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<CustomAdminPage>>(dsCustomPages_SelectData);
        dsCustomPages2.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<CustomAdminPage>>(dsCustomPages_SelectData);
        if (!IsPostBack)
        {
            if (CustomPage != null)
            {
                this.Title = CustomPage.Name;
                lblTitleHeader.Text = CustomPage.Name;
                lblTitleContent.Text = CustomPage.Notes;
            }
        }
    }

    void dsCustomPages_SelectData(object sender, Acms.DataControls.SelectEventArgs<CustomAdminPage> e)
    {
        e.SetData(AcmsContext.Site.CustomAdminPages);
    }

    public CustomAdminPage CustomPage { get; set; }
    protected string VirtualControlPath { get; set; }

    protected override object SaveViewState()
    {
        ViewState["VirtualControlPath"] = VirtualControlPath;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        VirtualControlPath = ViewState["VirtualControlPath"] as string;
    }
}
